package com.shine.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shine.demo.entity.SysUser;
import com.shine.demo.mapper.UserMapper;
import com.shine.demo.common.enums.StatusEnum;
import com.shine.demo.common.result.Result;
import com.shine.demo.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements IUserService {

    @Resource
    UserMapper userMapper;

    @Override
    public Result getUserName(String name) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, name);
        SysUser sysUser = userMapper.selectOne(queryWrapper);

        return Result.success(sysUser);
    }


    @Override
    public Result demotion(String name) {
        if("error".equals(name)){
            throw new RuntimeException("出错了!");
        }
        if("sleep".equals(name)){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        JSONObject o = new JSONObject();
        o.put("name",name);
        return Result.success(o);
    }

    @Override
    public Result register(SysUser user) {
        user.setStatus(StatusEnum.NATURAL.getCode());
        userMapper.insert(user);
        return Result.success();
    }
}
